package com.net.xpay.admin.controller.admin;

import com.google.common.base.Preconditions;
import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.vo.FrozenUserLogVo;
import com.net.xpay.common.domain.FrozenUserLog;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.manager.FrozenUserLogManager;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.partner.PartnerManager;
import com.net.xpay.common.model.search.FrozenUserLogSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 */
@RestController
@Slf4j
@RequestMapping("/admin/frozen-user-log")
@Api(tags = "后台/冻结用户记录", description = "后台 冻结用户记录")
public class AdminFrozenUserLogController {

    @Autowired
    private FrozenUserLogManager frozenUserLogManager;

    @Autowired
    private UserManager userManager;

    @Autowired
    private PartnerManager partnerManager;

    /**
     * 分页
     */
    @ApiOperation("分页")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'frozen-user-log::search')")
    public ResultPagedList<FrozenUserLogVo> search(@RequestBody FrozenUserLogSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        List<FrozenUserLog> records = frozenUserLogManager.search(searchModel);
        List<FrozenUserLogVo> vos = BeanCopierUtil.batchCopy(FrozenUserLog.class, FrozenUserLogVo.class, records);

        for (FrozenUserLogVo vo : vos) {
            User user = userManager.getById(vo.getUserId());
            vo.setUserName(user.getName());

            if (vo.getPartnerId() != null) {
                Partner partner = partnerManager.getById(vo.getPartnerId());
                vo.setPartnerName(partner.getName());
            }
        }
        long count = frozenUserLogManager.searchCount(searchModel);

        return new ResultPagedList<>(vos, count, searchModel);


    }




}
